package com.api.gbhg.huangwm.dao;

import com.customization.commons.LogTool;
import org.apache.commons.lang.StringUtils;
import org.eclipse.swt.widgets.Link;
import weaver.conn.RecordSet;
import weaver.hrm.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Huangwm
 * @Date 2022-04-24
 * @Version 1.0
 * @Description TODO 项目预算操作修改
 **/
public class ProjectOperationDao {
    private LogTool logTool = new LogTool("/log/gbhg/huangwm/dao/ProjectOperationDao",false);

    /**
     * 根据用户id和项目流程id查询是否有权限
     * @param user
     * @param requestid
     * @return
     */
    public Map<String,Object> hasRight(User user, String requestid){
        Map<String,Object> result = new HashMap<>();
        if(user==null || requestid.isEmpty()){
            result.put("apiStatus",true);
            result.put("hasRight",false);
            result.put("msg","用户或对应的项目流程id为空");
            return result;
        }
        RecordSet rs = new RecordSet();
        if(user.getUserDepartment()==221 || user.getUID()==13939){
            //预算管理科的人有权限 政务云测试账号也有权限
            rs.execute(" select * from uf_jingfei_version where lcjlsqjlid="+requestid);
            rs.next();
            result.put("apiStatus",true);
            result.put("sydwids",rs.getString("sydw"));
            result.put("sydwnames",getSubcompanyNames(rs.getString("sydw")));
            result.put("hasRight",true);
            return result;
        }
        //预算经办人也有权限
        rs.execute(" select * from uf_jingfei_version where lcjlsqjlid="+requestid+" and jbr="+user.getUID());
        if(rs.next()){
            result.put("apiStatus",true);
            result.put("sydwids",rs.getString("sydw"));
            result.put("sydwnames",getSubcompanyNames(rs.getString("sydw")));
            result.put("hasRight",true);
            return result;
        }else{
            RecordSet rs1 = new RecordSet();
            //由于经办人可能单位调动 再查询下授权里的预算编制人是否包含该单位
            String sql = " select bmxfdcs from uf_dwysbzrywh where ysbzjbr like'%"+user.getUID()+"%'";
            rs1.execute(sql);
            if(rs1.next()){
                rs.execute("select * from uf_jingfei_version where lcjlsqjlid="+requestid);
                rs.next();
                String sbdw = rs.getString("sbdw");
                if(rs1.getString("bmxfdcs").equals(sbdw)){
                    result.put("apiStatus",true);
                    result.put("sydwids",rs.getString("sydw"));
                    result.put("sydwnames",getSubcompanyNames(rs.getString("sydw")));
                    result.put("hasRight",true);
                    return result;
                }
            }else{
                result.put("apiStatus",true);
                result.put("hasRight",false);
                result.put("msg","当前用户不具备此项目修改权限");
                return result;
            }
        }
        return result;
    }

    /**
     * 根据分部id集合获取所有分部名称
     * @param ids
     * @return
     */
    public String getSubcompanyNames(String ids){
        if(ids.isEmpty()) return "";
        RecordSet rs = new RecordSet();
        String name = "";
        rs.execute("select * from hrmsubcompany where id in("+ids+")");
        while (rs.next()){
            name += rs.getString("subcompanyname")+",";
        }
        return name.substring(0,name.length()-1);
    }


    /**
     * 根据requestid获取流程表名
     * @param requestid
     * @return
     */
    public String getWorkflowTabNameByRequestid(RecordSet rs,String requestid){
        String sql = "select tablename from workflow_bill where id = (select formid from workflow_base where id =(select workflowid from workflow_requestbase where requestid="+requestid+"))";
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("tablename");
    }

    /**
     * 根据流程id获取流程表名
     * @param workflowId
     * @return
     */
    public String getWorkflowTabNameByWfid(String workflowId){
        RecordSet rs = new RecordSet();
        String sql = "select tablename from workflow_bill where id = (select formid from workflow_base where id = "+workflowId+")";
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("tablename");
    }

    /**
     * 根据流程主表id获取流程明细表名称列表
     * @param maindId
     * @return
     */
    public static List<String> getDetailTables(int maindId) {
        List<String> resList = new ArrayList<>();
        RecordSet rs = new RecordSet();
        rs.executeQuery("select d.tablename from workflow_billdetailtable d where d.billid=" + maindId + ";");
        while (rs.next()) {
            resList.add(rs.getString("tablename"));
        }
        return resList;
    }

    /**
     * 查询单位名称
     * @param id
     * @return
     */
    public String getSubcompanyNameById(String id){
        RecordSet rts=new RecordSet();
        String sql="select subcompanyname from hrmsubcompany where id="+id;
        rts.executeQuery(sql);
        rts.next();
        return rts.getString("subcompanyname");
    }

    /**
     * 查询科室名称
     * @param id
     * @return
     */
    public String getDepartmentNameById(String id){
        RecordSet rts=new RecordSet();
        String sql="select departmentname from HrmDepartment where id="+id;
        rts.executeQuery(sql);
        rts.next();
        return rts.getString("departmentname");
    }

    /**
     * 查询用户名称
     * @param id
     * @return
     */
    public String getUserNameById(String id){
        RecordSet rts=new RecordSet();
        String sql="select lastname from hrmresource where id="+id;
        rts.executeQuery(sql);
        rts.next();
        return rts.getString("lastname");
    }

    /**
     * 根据流程配置id，获取流程表id
     * @param workFlowId 流程配置id
     * @return
     */
    public int getFlowTableLableId(String workFlowId) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select w.formid from workflow_base w where w.id=" + workFlowId);
        rs.next();
        return rs.getInt("formid");
    }

    public  List<Map<String, Object>> getProjectList2Zyczbksr1(Map<String,Object> params){
        List<Map<String, Object>> list1 = new ArrayList<>();
        String ysnd = params.get("ysnd").toString();

        YuSuanTiaoZhengDao dao = new YuSuanTiaoZhengDao();
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSet rs3 = new RecordSet();
        try{
            //查询财政拨款 当年预算核批的单渠道项目
            String sql1 = "select lcjlsqjlid,xmmin,ysnd,sbdw,yszje from uf_jingfei_version where lcztz='3' and dlhsdw=1 and ysnd="+ysnd;
            rs1.execute(sql1);
            while (rs1.next()){
                Map<String, Object> data = new HashMap<>();
                String lcjlsqjlid = rs1.getString("lcjlsqjlid");
                data.put("xmid",lcjlsqjlid);
                data.put("xmmc",rs1.getString("xmmin"));
                data.put("ysnd",ysnd);
                data.put("dlhsdw","拱北海关");
                data.put("sbdw",getSubcompanyNameById(rs1.getString("sbdw")));
                data.put("yszje",rs1.getString("yszje"));
                String sql2 = "select * from uf_jingfeilaiyuan where  srlx=0 and yslylx='当年预算核批' and lcid="+lcjlsqjlid;
                rs2.execute(sql2);
                if(rs2.getCounts()==1){
                    rs2.next();
                    String zyczbksr = rs2.getString("zyczbksr");
                    String je = rs2.getString("je");
                    data.put("zjqd",dao.getZyczbksrName(zyczbksr)+"(核批金额:"+je+")");

                    String sql3 = "select bmzcjjfl,ze from uf_jingfeiVersion where lcjlid="+lcjlsqjlid;
                    rs3.execute(sql3);
                    List<String> bmjjfl = new ArrayList<>();
                    while (rs3.next()){
                        bmjjfl.add(rs3.getString("bmzcjjfl")+"(核批额度:"+rs3.getString("ze")+")");
                    }
                    data.put("bmjjfl", StringUtils.join(bmjjfl,";<br />"));

                    list1.add(data);
                }else{
                    continue;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("获取项目列表异常 "+e.getMessage());
        }
        return list1;
    }

    public  List<Map<String, Object>> getProjectList2Zyczbksr2(Map<String,Object> params){
        List<Map<String, Object>> list2 = new ArrayList<>();
        String ysnd = params.get("ysnd").toString();

        YuSuanTiaoZhengDao dao = new YuSuanTiaoZhengDao();
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSet rs3 = new RecordSet();
        try{
            //查询财政拨款 当年预算核批的单渠道项目
            String sql1 = "select lcjlsqjlid,xmmin,ysnd,sbdw,yszje  from uf_jingfei_version where lcztz='3' and dlhsdw=1 and ysnd="+ysnd;
            rs1.execute(sql1);
            while (rs1.next()){
                Map<String, Object> data = new HashMap<>();
                String lcjlsqjlid = rs1.getString("lcjlsqjlid");
                data.put("xmid",lcjlsqjlid);
                data.put("xmmc",rs1.getString("xmmin"));
                data.put("ysnd",ysnd);
                data.put("dlhsdw","拱北海关");
                data.put("sbdw",getSubcompanyNameById(rs1.getString("sbdw")));
                data.put("yszje",rs1.getString("yszje"));
                String sql2 = "select * from uf_jingfeilaiyuan where  srlx=0 and yslylx='当年预算核批' and lcid="+lcjlsqjlid;
                rs2.execute(sql2);
                if(rs2.getCounts()>1){
                    List<String> zjqd = new ArrayList<>();
                    while (rs2.next()){
                        String zyczbksr = rs2.getString("zyczbksr");
                        String je = rs2.getString("je");
                        zjqd.add(dao.getZyczbksrName(zyczbksr)+"(核批金额:"+je+")");
                    }
                    data.put("zjqd",StringUtils.join(zjqd,";<br />"));
                    String sql3 = "select bmzcjjfl,ze from uf_jingfeiVersion where lcjlid="+lcjlsqjlid;
                    rs3.execute(sql3);
                    List<String> bmjjfl = new ArrayList<>();
                    while (rs3.next()){
                        bmjjfl.add(rs3.getString("bmzcjjfl")+"(核批额度:"+rs3.getString("ze")+")");
                    }
                    data.put("bmjjfl", StringUtils.join(bmjjfl,";<br />"));

                    list2.add(data);
                }else{
                    continue;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("获取项目列表异常 "+e.getMessage());
        }
        return list2;
    }
}
